<?php
	class Controller extends GlobalController
	{
	    /**
		 * Is loaded before any controller and performs common for all controllers actions
		 *
		 * @param array $url page url, devided into controller, action and parameters 
		 */			
		function BeforeLoad($url)
		{
			//add menu
			$this->IncludeElement('menus');

			if (empty($_SESSION[adminid])&&($url[action])!='login')
			{
				$_SESSION[redirect_url] = $_SERVER['REQUEST_URI'];
				header ("Location: /spanel/main/login");
			}
			//validation errors
			//$this->data[validation_errors] = $this->controller_model->validation_errors;	
		}
		
	    /**
		 * Is loaded after any controller and performs common for all controllers actions
		 *
		 * @param array $url page url, devided into controller, action and parameters 
		 */			
		function AfterLoad($url)
		{
		
		}	
		
	    /**
		 * Gets the file names' list in the given directory
		 *
		 * @param string $dir_path path to the files' location 
		 * @param string $extension if specified, indicates whet extension to delete from the file name. 
		*/			
		function GetFileNames($dir_path, $extension = null)
		{
			if (!is_dir($dir_path)) return false;
			
			$files = scandir($dir_path);

			$items = array();
			foreach ($files as $file)
			{
				//if the file doesn't match the specified extension
		        if ((!empty($extension))&&(!ereg(".$extension$", $file))) continue;
		        
				if ($file != '.' && $file != '..' && $file !="CVS") 
		        {
		        	if (!empty($extension)) $items[] = eregi_replace("\.$extension$", '', $file); 
		        	else $items[] = $file;
		        }
		    }
		    unset($files);
		    return $items;
		}		
		
		/**
		 * Creates file explorer menu
		 *
		 */
		function CreateFileExplorerMenu()
		{
			if (!isset($_SESSION[file_explorer]))
			{
				//file explorer
				$file_explorer = array();
				File::ScanDirectories('.', array('php', 'css', 'js', 'html', 'htm', 'bak'), array('CVS', '.svn'), $file_explorer);
				
				$file_explorer = $this->GetFileExplorerMenu($file_explorer);
				
				//add file explorer to the root element
				$root_file_explorer[begin][name] = ucfirst(LANG_FILE_EXPLORER);
				$root_file_explorer[begin][url] = null;
				$root_file_explorer[begin][sub] = $file_explorer;

				//display tree menu
				$_SESSION[file_explorer] = TreeMenu::CreateMenu($root_file_explorer);
			}
			echo "<br /><hr />";
			echo '<div id = "file_explorer_div">'.$_SESSION[file_explorer].'</div>';
		}
		
		/**
		 * Refresh session arrays
		 *
		 */
		function RefreshSession()
		{
			$this->layout = 'blank';
			
			unset($_SESSION[main_menu]);
			unset($_SESSION[file_explorer]);
			unset($_SESSION[spherusPHPToolbar]);
			unset($_SESSION[highlightFunctionString]);
		}

		/**
		 * Saves project explorer into the session
		 * 
		 */
		function SaveProjectExplorer()
		{
			$_SESSION[main_menu] = $this->request->post[content];
		}
		
		/**
		 * Updates File structure explorer, is used with AJAX
		 *
		 */
		function UpdateFileExplorerMenu()
		{
			$this->layout = 'blank';
			echo "@";
			//file explorer
			$file_explorer = array();
			File::ScanDirectories('.', array('php', 'css', 'js', 'html'), array('CVS'), $file_explorer);
			
			$file_explorer = $this->GetFileExplorerMenu($file_explorer);
			
			//add file explorer to the root element
			$root_file_explorer[begin][name] = ucfirst(LANG_FILE_EXPLORER);
			$root_file_explorer[begin][url] = null;
			$root_file_explorer[begin][sub] = $file_explorer;

			//display tree menu
			$_SESSION[file_explorer] = TreeMenu::CreateMenu($root_file_explorer);

			echo "<br /><hr />";
			echo $_SESSION[file_explorer];
			exit();
		}
		
		/**
		 * Converts file structure array to the TreeMenu compatible array
		 *
		 * @param array $file_explorer
		 * @return converted array
		 */
		function GetFileExplorerMenu(&$file_explorer)
		{
			foreach ($file_explorer as &$elem)
			{
				if (!empty($elem['sub']))
				{
					$this->GetFileExplorerMenu($elem['sub']);
				}
				else
				{
					$elem[url] = '/spanel/viewsource/index/?path_to_file='.$elem['path'].SEPARATOR.$elem['name'];
				}
			}
			return $file_explorer;
		}
	}
?>